package com.fr.report.script.function;

import com.fr.base.core.ComparatorUtils;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;

public class INDEX extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 1)
      return Primitive.ERROR_NAME;
    Object localObject = paramArrayOfObject[0];
    int i = 1;
    for (int j = 1; j < paramArrayOfObject.length; ++j)
    {
      if (ComparatorUtils.equals(localObject, paramArrayOfObject[j]))
        break;
      ++i;
    }
    return new Integer(i);
  }

  public Function.Type getType()
  {
    return Function.OTHER;
  }

  public String getCN()
  {
    return "INDEX(key,val1,val2,...):返回key在val1,val2,...所组成的序列中的位置,不存在于序列中则返回参数的个数.\n备注:\n    key和valn可以是任意类型\n示例:\nINDEX(2,2)等于1。\nINDEX(2,1,2)等于2。\nINDEX(2,4,5,6)等于4。\nINDEX(\"b\",\"b\",\"o\",\"y\")等于1。";
  }

  public String getEN()
  {
    return "INDEX(key,val1,val2,...):return the position that key in val1,val2,..., if not exist, return the length of arguments\nkey and valn can be of any type.\n\nExamples:\nINDEX(2,2) = 1。\nINDEX(2,1,2) = 2。\nINDEX(2,4,5,6) = 4。\nINDEX(\"b\",\"b\",\"o\",\"y\") = 1。";
  }
}